/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pig.mt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.mt.common.api.utils.Query;
import com.pig4cloud.pig.mt.entity.WxQrCode;
import com.pig4cloud.pig.mt.form.WxQrCodeForm;
import com.pig4cloud.pig.mt.mapper.WxQrCodeMapper;
import com.pig4cloud.pig.mt.service.WxQrCodeService;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

/**
 * 公众号带参二维码
 *
 * @author JLM
 * @date 2021-03-05 11:45:09
 */
@Service
@RequiredArgsConstructor
public class WxQrCodeServiceImpl extends ServiceImpl<WxQrCodeMapper, WxQrCode> implements WxQrCodeService {
	private final WxMpService wxService;

	@Override
	public IPage queryPage(Map<String, Object> params) {
		String sceneStr = (String) params.get("sceneStr");
		String appid = (String) params.get("appid");
		IPage<WxQrCode> page = this.page(
				new Query<WxQrCode>().getPage(params),
				new QueryWrapper<WxQrCode>()
						.eq(!StringUtils.isEmpty(appid), "appid", appid)
						.like(!StringUtils.isEmpty(sceneStr), "scene_str", sceneStr)
		);

		return page;
	}

	/**
	 * 创建公众号带参二维码
	 *
	 *
	 * @param appid
	 * @param form
	 * @return
	 */
	@Override
	public WxMpQrCodeTicket createQrCode(String appid, WxQrCodeForm form) throws WxErrorException {
		WxMpQrCodeTicket ticket;
		if (form.getIsTemp()) {//创建临时二维码
			ticket = wxService.getQrcodeService().qrCodeCreateTmpTicket(form.getSceneStr(), form.getExpireSeconds());
		} else {//创建永久二维码
			ticket = wxService.getQrcodeService().qrCodeCreateLastTicket(form.getSceneStr());
		}
		WxQrCode wxQrCode = new WxQrCode(form,appid);
		wxQrCode.setTicket(ticket.getTicket());
		wxQrCode.setUrl(ticket.getUrl());
		if (form.getIsTemp()) {
			wxQrCode.setExpireTime(new Date(System.currentTimeMillis() + ticket.getExpireSeconds() * 1000L));
		}
		this.save(wxQrCode);
		return ticket;
	}
}
